package com.fzc.rhinooffice.common.view;

import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.fzc.rhinooffice.R;

/**
 * Created by cong on 15/6/4.
 */
public class PullRefreshListView extends ListView {

    /**
     * 使用方法：
     * 1、setPullLoadMoreListener，设置下拉刷新的监听
     *
     * 2、在完成数据加载之后调用endRefresh()
     * */

    public final View footer;
    private int lastItem;
    private boolean loadFinish=true;
    private PullRefreshListView.PullLoadMoreListener moreListener;
    private Context mContext;

    public PullRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.mContext = context;
        footer = ((Activity) context).getLayoutInflater().inflate(R.layout.footer, null);
        this.addFooterView(footer);

        this.setOnScrollListener(new OnScrollListener() {
            //AbsListView view 这个view对象就是listview
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        if (loadFinish) {
                            loadFinish=false;//这个要自己处理了
                            footer.setVisibility(VISIBLE);
                            moreListener.loadMore();
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItem = firstVisibleItem + visibleItemCount - 1;
            }
        });

    }

    public void setPullLoadMoreListener(PullRefreshListView.PullLoadMoreListener listener) {
        this.moreListener = listener;
    }

    public interface PullLoadMoreListener {
        void loadMore();
    }

    public void startRefresh(){
        loadFinish=true;
        footer.setVisibility(GONE);
    }

    public void endRefresh() {
        loadFinish=true;
        footer.setVisibility(GONE);
    }

    /**
     * 模拟用户手势下拉
     * */
    public void startAutoRefresh(final View v){
        new Thread() {
            @Override
            public void run() {
                final long downTime = SystemClock.currentThreadTimeMillis();
                ((Activity)mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MotionEvent down = MotionEvent.obtain(downTime, SystemClock.currentThreadTimeMillis(), MotionEvent.ACTION_DOWN, 50, 100, 0);
                        v.dispatchTouchEvent(down);
                    }
                });
                SystemClock.sleep(50);
                ((Activity)mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MotionEvent move = MotionEvent.obtain(downTime, SystemClock.currentThreadTimeMillis(), MotionEvent.ACTION_MOVE, 50, 500, 0);
                        v.dispatchTouchEvent(move);
                    }
                });
                SystemClock.sleep(50);
                ((Activity)mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MotionEvent up = MotionEvent.obtain(downTime, SystemClock.currentThreadTimeMillis(), MotionEvent.ACTION_UP, 50, 550, 0);
                        v.dispatchTouchEvent(up);
                    }
                });
            }
        }.start();

    }
}
